home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b12 / Util / EventUtil.c < prev    next >
Text File  |  1995-12-09  |  13KB  |  553 lines

  1. /*****
  2.  *
  3.  *    Events.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp1.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompileWithDragNDrop
  18. #include <Drag.h>
  19. #endif
  20. //#include <Threads.h>
  21.  
  22. #include "compiler_stuff.h"
  23. #include "constants.h"
  24. #include "globals.h"
  25.  
  26. #include "AboutBox.h"
  27. #include "DebugUtil.h"
  28. #include "ErrorUtil.h"
  29. #include "MenuFunc.h"
  30. //#include "ProcessUtil.h"
  31. #include "WindowInt.h"
  32.  
  33. #include "EventUtil.h"
  34.  
  35.  
  36. /***  LOCAL PROTOTYPES ***/
  37.  
  38. static     void    doKeyPress    ( EventRecord * );
  39.  
  40.  
  41. /***  FUNCTIONS  ***/
  42.  
  43. /* 
  44.     ->message    long    the high-level event class to which the event belongs
  45.     ->when        long    time when event was posted (in ticks since system startup)
  46.     ->where        Point    the high-level event ID (cast as OSType)
  47.     ->modifiers    int        state of modifier keys and mouse button at event time */
  48. void
  49. doHighLevelEvent ( EventRecord *theEvent )
  50. {
  51.     OSErr        theErr;
  52.     
  53.     #if kCompileWithThreadedAppleEvents
  54.     ThreadID    theThread;
  55.     #endif
  56.     
  57.     switch ( theEvent->message )
  58.     {
  59.         /* you may insert checks for high level events other than apple events */
  60.         
  61.         /* default type is apple event */
  62.         default :
  63.             theErr = AEProcessAppleEvent ( theEvent );
  64.             if ( theErr != noErr )
  65.             {
  66.                 /* you might want to add error handling here.
  67.                     However, I advise against putting user interaction handling here,
  68.                     it can confuse the user. You might try using an assertion. */
  69.             }
  70.             break;
  71.     }
  72. } /* doHighLevelEvent */
  73.  
  74.  
  75. /**  USER INTERFACE  **/
  76. #pragma mark -
  77. #if kCompileWithForeground
  78.  
  79. /* 
  80.     ->message    long    undefined
  81.     ->when        long    time when event was posted (in ticks since system startup)
  82.     ->where        Point    cursor location at event time
  83.     ->modifiers    int        state of modifier keys and mouse button at event time
  84.                         and wether the mouse-down caused the application to come to the foreground
  85.     IM-MTE: 2-34 */
  86. void
  87. doMouseDown ( EventRecord *theEvent )
  88. {
  89.     WindowPtr     theWindow;
  90.     short          where;
  91.     Boolean        onItem;
  92.     #if kCompileWithDragNDrop
  93.     OSErr        theErr;
  94.     #endif
  95.     
  96.     #if kCompileWithModelessDialogs
  97.     /* modeless dialogs */
  98.     Boolean        dialogEvent;
  99.     Boolean        dialogResult;
  100.     DialogPtr    theDialog;
  101.     short        itemHit;
  102.     
  103.     /* determine whether the event is for a modeless dialog */
  104.     dialogEvent = IsDialogEvent ( theEvent );
  105.  
  106.     if ( dialogEvent )
  107.     {
  108.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  109.     }
  110.     else
  111.     {
  112.     #endif
  113.     
  114.         where = FindWindow ( theEvent->where, &theWindow );
  115.         
  116.         switch ( where ) /* where the event occurred */
  117.         {
  118.             case inDesk :
  119.                 break;
  120.             
  121.             case inMenuBar :
  122.                 adjustMenus    ();
  123.                 doMenu        ( MenuSelect(theEvent->where), theEvent->modifiers );
  124.                 break;
  125.             
  126.             case inSysWindow :
  127.                 /* click in a desk accessory window */
  128.                 SystemClick ( theEvent, theWindow );
  129.                 break;
  130.         
  131.             case inContent :
  132.                 /* content region of a window */
  133.                 if ( (theWindow != FrontWindow()) || !(((WindowPeek)theWindow)->hilited) )
  134.                 {
  135.                     switch ( WindowType(FrontWindow()) )
  136.                     {
  137.                         case Window_dlgModal :
  138.                         case Window_dlgMoveableModal :
  139.                             /* beep because can't switch from modal dialog */
  140.                             SysBeep ( 30 );
  141.                             break;
  142.                         
  143.                         case Window_UNKNOWN :
  144.                             my_assert ( kForceAssert, "\pdoMouseDown: Unknown window type" );
  145.                         
  146.                         case Window_about :
  147.                         case Window_DA :
  148.                         default :
  149.                             SelectWindow ( theWindow );
  150.                             break;
  151.                     }
  152.                 }
  153.                 else
  154.                 {
  155.                     onItem = false;
  156.                     WindowContentClick ( theWindow, theEvent, &onItem );
  157.                     
  158.                     #if kCompileWithDragNDrop
  159.                     if ( gHasDragNDrop && onItem && (WaitMouseMoved(theEvent->where)) )
  160.                     {
  161.                         theErr = MyDoStartDrag ( theEvent, theWindow );
  162.                     }
  163.                     #endif
  164.                 }
  165.                 break;
  166.     
  167.             case inDrag :
  168.                 /* title bar region of a window */
  169.                 switch ( WindowType(theWindow) )
  170.                 {
  171.                     case Window_dlgMoveableModal :
  172.                         if ( (theWindow == FrontWindow()) || (theEvent->modifiers & cmdKey) )
  173.                         {
  174.                             DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  175.                         }
  176.                         else
  177.                         {
  178.                             /* beep because can't switch from moveable modal dialog */
  179.                             SysBeep ( 30 );
  180.                         }
  181.                         break;
  182.                     
  183.                     default :
  184.                         /* if the command key is pressed, don't activate the window, just drag it */
  185.                         if ( !(theEvent->modifiers & cmdKey) )
  186.                         {
  187.                             SelectWindow ( theWindow );
  188.                         }
  189.                         
  190.                         DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  191.                         break;
  192.                 }
  193.                 break;
  194.         
  195.             case inGrow :
  196.                 /* grow box region of a window (lower right hand corner) */
  197.                 WindowGrow ( theWindow, theEvent->where );
  198.                 break;
  199.                 
  200.             case inZoomIn:
  201.             case inZoomOut:
  202.                 /* zoom box of a window (upper right hand corner) */
  203.                 if ( TrackBox(theWindow, theEvent->where, where) )
  204.                 {
  205.                     WindowZoomBox ( theWindow, where );
  206.                 }
  207.                 break;
  208.         
  209.             case inGoAway :
  210.                 /* close box of a window (upper left hand corner) */
  211.                 if ( TrackGoAway ( theWindow, theEvent->where ) )
  212.                 {
  213.                     WindowClose ( theWindow, theEvent->modifiers, true );
  214.                 }
  215.                 break;
  216.         }
  217.         
  218.     #if kCompileWithModelessDialogs
  219.     }
  220.     #endif
  221. } /* doMouseDown */
  222.  
  223.  
  224. /* 
  225.     ->message    long    undefined
  226.     ->when        long    time when event was posted (in ticks since system startup)
  227.     ->where        Point    cursor location at event time
  228.     ->modifiers    int        state of modifier keys and mouse button at event time */
  229. void
  230. doMouseUp ( EventRecord *theEvent )
  231. {
  232.     #if kCompileWithModelessDialogs
  233.     /* modeless dialogs */
  234.     Boolean        dialogEvent;
  235.     Boolean        dialogResult;
  236.     DialogPtr    theDialog;
  237.     short        itemHit;
  238.     
  239.     /* determine whether the event is for a modeless dialog */
  240.     dialogEvent = IsDialogEvent ( theEvent );
  241.  
  242.     if ( dialogEvent )
  243.     {
  244.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  245.     }
  246.     else
  247.     {
  248.     }
  249.     #endif
  250. } /* doMouseUp */
  251.  
  252.  
  253. /**  Keyboard Events  **/
  254. #pragma mark -
  255.  
  256. /* 
  257.     ->message    long    char and virtual key codes in low-order word.
  258.                         ADB address in low byte of high-order word
  259.     ->when        long    time when event was posted (in ticks since system startup)
  260.     ->where        Point    cursor location at event time
  261.     ->modifiers    int        state of modifier keys and mouse button at event time
  262.     IM-MTE 2-44 */
  263. void
  264. doKeyDown ( EventRecord *theEvent )
  265. {
  266.     char    theChar;
  267.     
  268.     /* the character being pressed is in the message of the event record */
  269.     theChar = theEvent->message & charCodeMask;
  270.     
  271.     if ( theEvent->modifiers & cmdKey )
  272.     {
  273.         /* if the command key is being held down, treat as menu request */
  274.         adjustMenus    ();
  275.         doMenu        ( MenuKey (theChar), theEvent->modifiers );
  276.     }
  277.     else
  278.     {
  279.         /* handle regular keypress */
  280.         doKeyPress ( theEvent );
  281.     }
  282. } /* doKeyDown */
  283.  
  284.  
  285. /* 
  286.     ->message    long    char and virtual key codes in low-order word.
  287.                         ADB address in low byte of high-order word
  288.     ->when        long    time when event was posted (in ticks since system startup)
  289.     ->where        Point    cursor location at event time
  290.     ->modifiers    int        state of modifier keys and mouse button at event time */
  291. void
  292. doAutoKey ( EventRecord *theEvent )
  293. {
  294.     if ( theEvent->modifiers & cmdKey )
  295.     {
  296.         /* don't do anything. Command keys should not auto-repeat. */
  297.     }
  298.     else
  299.     {
  300.         /* handle regular keypress */
  301.         doKeyPress ( theEvent );
  302.     }
  303. } /* doAutoKey */
  304.  
  305.  
  306. /* 
  307.     ->message    long    char and virtual key codes in low-order word.
  308.                         ADB address in low byte of high-order word
  309.     ->when        long    time when event was posted (in ticks since system startup)
  310.     ->where        Point    cursor location at event time
  311.     ->modifiers    int        state of modifier keys and mouse button at event time */
  312. void
  313. doKeyUp ( EventRecord *theEvent )
  314. {
  315.     
  316. } /* doKeyUp */
  317.  
  318.  
  319. /* handle regular keypress
  320.     ->message    long    char and virtual key codes in low-order word.
  321.                         ADB address in low byte of high-order word
  322.     ->when        long    time when event was posted (in ticks since system startup)
  323.     ->where        Point    cursor location at event time
  324.     ->modifiers    int        state of modifier keys and mouse button at event time
  325.     IM-MTE: 2-44,45 */
  326. static void
  327. doKeyPress ( EventRecord *theEvent )
  328. {
  329.     /* If you add windows that allow for text entry or you accept non-command-key
  330.         keyboard input, you need to support this function. */
  331.     #if kCompileWithKeyboardEvents
  332.     MyKeyPress ( theEvent );
  333.     #endif
  334. } /* doKeyPress */
  335.  
  336.  
  337. /**  Other Events  **/
  338. #pragma mark -
  339.  
  340. /* Handle activate event - a window has been brought to front, or a window
  341.     that was in front is no longer.
  342.     ->message    long    window ptr for window to de/activate
  343.     ->when        long    time when event was posted (in ticks since system startup)
  344.     ->where        Point    cursor location at event time
  345.     ->modifiers    int        state of modifier keys and mouse button at event time
  346.                         and whether window should be activated or deactivated
  347.     IM-MTE: 2-50-55 */
  348. void
  349. doActivateEvent ( EventRecord *theEvent )
  350. {
  351.     WindowPtr    theWindow;
  352.     Boolean        becomingActive;
  353.     
  354.     theWindow        = (WindowPtr) theEvent->message;
  355.     becomingActive    = (theEvent->modifiers & activeFlag) != nil;
  356.     
  357.     switch ( WindowType(theWindow) )
  358.     {
  359.         case Window_none :
  360.             break;
  361.         
  362.         case Window_UNKNOWN :
  363.             my_assert ( kForceAssert, "\pdoActivateEvent: Unknown window type" );
  364.             break;
  365.         
  366.         default :
  367.             WindowActivate ( theWindow, becomingActive, theEvent );
  368.             break;
  369.     }
  370. } /* doActivateEvent */
  371.  
  372.  
  373. /* A window needs to be updated.
  374.     ->message    long    window ptr for window to update
  375.     ->when        long    time when event was posted (in ticks since system startup)
  376.     ->where        Point    cursor location at event time
  377.     ->modifiers    int        state of modifier keys and mouse button at event time
  378.     IM-MTE: 2-47-50 */
  379. void
  380. doUpdateEvent ( EventRecord *theEvent )
  381. {
  382.     WindowPtr        theWindow;
  383.     
  384.     theWindow    = (WindowPtr) (theEvent->message);
  385.     
  386.     if ( theWindow != nil )
  387.     {
  388.         WindowUpdate ( theWindow );
  389.     }
  390. } /* doUpdateEvent */
  391.  
  392.  
  393. /*
  394.     ->message    long    window ptr for window to de/activate
  395.     ->when        long    time when event was posted (in ticks since system startup)
  396.     ->where        Point    cursor location at event time
  397.     ->modifiers    int        state of modifier keys and mouse button at event time
  398.                         and whether window should be activated or deactivated
  399.     IM-MTE: 2-58-62 */
  400. void
  401. doOsEvt ( const EventRecord *theEvent )
  402. {
  403.     WindowPtr    theWindow;
  404.     
  405.     switch ( (theEvent->message >> 24) & 0x000000FF )    /* should have a constant here */
  406.     {
  407.         case suspendResumeMessage :
  408.             gFrontProcess = theEvent->message & resumeFlag;
  409.             
  410.             theWindow = FrontWindow ();
  411.             
  412.             if ( gFrontProcess )
  413.             {
  414.                 /* resume event */
  415.                 
  416.                 if ( theEvent->message & convertClipboardFlag )
  417.                 {
  418.                     /* convert the clipboard */
  419.                 }
  420.                 
  421.                 switch ( WindowType(theWindow) )
  422.                 {
  423.                     case Window_none :
  424.                         break;
  425.                     
  426.                     case Window_UNKNOWN :
  427.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  428.                     
  429.                     default :
  430.                         WindowActivate ( theWindow, true, theEvent );
  431.                         break;
  432.                 }
  433.             }
  434.             else
  435.             {
  436.                 /* suspend event */
  437.                 /* convert the clipboard, if needed, for export */
  438.                 
  439.                 switch ( WindowType(theWindow) )
  440.                 {
  441.                     case Window_none :
  442.                         break;
  443.                     
  444.                     case Window_UNKNOWN :
  445.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  446.                     
  447.                     default :
  448.                         WindowActivate ( theWindow, false, theEvent );
  449.                         break;
  450.                 }
  451.             }
  452.             break;
  453.         
  454.         case mouseMovedMessage : /* IM-MTE: 2-62-67 */
  455.             /* you may want to adjust your cursor and mouse region here */
  456.             break;
  457.     }
  458. } /* doOsEvt */
  459.  
  460.  
  461. /* Handle 'bad' disk mounts. Just call the system DIBadMount call.
  462.     ->message    long    drive number in low-order word, file manager result code in high-order word
  463.     ->when        long    time when event was posted (in ticks since system startup)
  464.     ->where        Point    cursor location at event time
  465.     ->modifiers    int        state of modifier keys and mouse button at event time
  466.     IM-MTE:2-55,56 */
  467. void
  468. doDiskEvt ( EventRecord *theEvent )
  469. {
  470.     Point    aPoint;
  471.     OSErr    theErr;
  472.     
  473.     /* if disk mount was unsuccessful */
  474.     if ( HiWord(theEvent->message) != noErr )
  475.     {
  476.         /* load disk initialization manager */
  477.         DILoad ();
  478.         
  479.         aPoint.v    = 120;
  480.         aPoint.h    = 120;
  481.         theErr        = DIBadMount ( aPoint, theEvent->message );
  482.         
  483.         /* unload disk initialization manager */
  484.         DIUnload ();
  485.     }
  486. } /* doDiskEvt */
  487.  
  488. #endif    /* kCompileWithForeground */
  489.  
  490. /**  Idle Time  **/
  491. #pragma mark -
  492.  
  493. /* 
  494.     ->message    long    undefined
  495.     ->when        long    time when event was posted (in ticks since system startup)
  496.     ->where        Point    cursor location at event time
  497.     ->modifiers    int        state of modifier keys and mouse button at event time
  498.     IM-MTE: 2-57,58, 6-79,80 */
  499. void
  500. doIdle ( EventRecord *theEvent )
  501. {
  502.     #if kCompileWithForeground
  503.     WindowPtr    theWindow;
  504.     short        itemHit;
  505.     Boolean        theResult;
  506.     #endif
  507.     
  508.     #if kCompileWithThreadsOptional
  509.     if ( gHasThreadMgr )
  510.     #endif
  511.     {
  512.         YieldToAnyThread ();
  513.     }
  514.     
  515.     #if kCompileWithQuitOnLongIdle
  516.     if ( gDoIdleQuit && (gTimeLastAction > nil) &&
  517.         ((gTimeLastAction + gIdleTimeToQuit) < TickCount()) )
  518.     {
  519.         gQuit = true;
  520.     }
  521.     #endif
  522.     
  523.     #if kCompileWithForeground
  524.     if ( gFrontProcess )
  525.     {
  526.         theWindow = FrontWindow ();
  527.         
  528.         switch ( WindowType(theWindow) )
  529.         {
  530.             case Window_about :
  531.             case Window_none :
  532.                 /* don't do anything */
  533.                 break;
  534.             
  535.             case Window_dlgModal :
  536.             case Window_dlgMoveableModal :
  537.                 /* IM:MTB 2-29,30 */
  538.                 theResult = DialogSelect ( theEvent, (DialogPtr *)&theWindow, &itemHit );
  539.                 break;
  540.             
  541.             case Window_UNKNOWN :
  542.                 my_assert ( kForceAssert, "\pdoIdle: unknown window type" );
  543.             
  544.             default :
  545.                 break;
  546.         }
  547.     }
  548.     #endif    /* kCompileWithForeground */
  549. } /* doIdle */
  550.  
  551.  
  552. /*****  EOF  *****/
  553.